package com.zqweb.gulimall.order.controller;

import com.zqweb.gulimall.order.entity.OrderEntity;
import com.zqweb.gulimall.order.entity.OrderReturnReasonEntity;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.UUID;

@RestController
public class RabbitController {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("/order/create/mqTest")
    @ResponseBody
    public String createOrder_MQTest(){
        OrderEntity orderEntity = new OrderEntity();
        orderEntity.setOrderSn(UUID.randomUUID().toString().replace("-",""));
        rabbitTemplate.convertAndSend("order-event-exchange","order.create.order",orderEntity);
        return "成功发出创建订单请求消息！";
    }

    @GetMapping("/sendMQ")
    public String sendMQ(@RequestParam(value = "num",defaultValue = "10") Integer num){
        for (int i = 0; i < num; i++) {
            if(i % 2 == 0){
                OrderReturnReasonEntity reasonEntity = new OrderReturnReasonEntity();
                reasonEntity.setId(Long.parseLong(i+""));
                reasonEntity.setName("钱包不足！");
                reasonEntity.setCreateTime(new Date());
                rabbitTemplate.convertAndSend("hello-rabbitmq-exchange","hello.rabbitmq",reasonEntity,new CorrelationData(UUID.randomUUID().toString()));
            }else{
                OrderEntity orderEntity = new OrderEntity();
                orderEntity.setCreateTime(new Date());
                orderEntity.setId(Long.parseLong(i+""));
                rabbitTemplate.convertAndSend("hello-rabbitmq-exchange","hello.rabbitmq",orderEntity,new CorrelationData(UUID.randomUUID().toString()));
            }
        }
        return "消息发送成功！";
    }


}
